package com.example.prom.qianduoduo.y2022.model;

import com.alibaba.fastjson.JSON;
import com.example.prom.strategy.excel.CustomCellStyleStrategy;
import com.example.prom.strategy.excel.CustomColumnWidthStrategy;
import lombok.Data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.excel.EasyExcelFactory.write;

@Data
public class ClinicaltrialsModel {
    private String draw;
    private String recordsTotal;
    private String recordsFiltered;
    private List<List<String>> data;

    public static void main(String[] args) throws IOException {
        String prefix = "https://clinicaltrials.gov/";
        String s = "{\"draw\":\"1\",\"recordsTotal\":\"17\",\"recordsFiltered\":\"17\",\"data\":[[\"<a id=\\\"rowId1\\\"></a>1\",\"NCT04448197\",\"<span style=\\\"color:#E60000;\\\">Completed</span>\",\"<a title=\\\"Show study NCT04448197: Insomnia Treated by Traditional Chinese Medicine and Its Circadian Rhythm\\\" href=\\\"/ct2/show/NCT04448197?term=Chinese+medicine&amp;cond=Insomnia&amp;cntry=CN&amp;draw=1&amp;rank=1\\\"><span class=\\\"hit_inf\\\">Insomnia</span> Treated by Traditional <span class=\\\"hit_inf\\\">Chinese Medicine</span> and Its Circadian Rhythm</a>\",\"<ul><li>Traditional <span class=\\\"hit_inf\\\">Chinese Medicine</span></li><li><span class=\\\"hit_inf\\\">Insomnia</span></li><li>Circadian Rhythm</li></ul>\",\"<ul><li>Other: <span class=\\\"hit_inf\\\">Chinese Medicine</span></li></ul>\",\"Observational<br/>\",\"\",\"<ul><li>The First Affiliated Hospital with Nanjing Medical University</li></ul>\",\"Other\",\"<ul><li>Observational Model: Other</li><li>Time Perspective: Prospective</li></ul>\",\"<ul><li>Change from Baseline in the total score of the Pittsburgh sleep quality index (PSQI) at Month 1</li><li>Change from Baseline in the total score of the 17-item Hamilton Depression Scale (HAMD-17) at Month 1</li><li>Change in of the ALT&#65292;to evaluate the safety of TCM treatment</li><li>(and 5 more...)</li></ul>\",\"50\",\"All\",\"18 Years to 75 Years   (Adult, Older Adult)\",\"NCT04448197\",\"2019-SR-484\",\"\",\"March 4, 2020\",\"October 16, 2020\",\"January 30, 2021\",\"June 25, 2020\",\"August 24, 2021\",\"\",\"<ul><li>The First Affiliated Hospital of Nanjing Medical University<br/>Nanjing, Jiangsu, <span class=\\\"hit_inf\\\">China</span></li></ul>\",\"\"]]}";
        ClinicaltrialsModel clinicaltrialsModel = JSON.parseObject(s, ClinicaltrialsModel.class);
        System.out.println(clinicaltrialsModel.toString());
        List<List<String>> data = clinicaltrialsModel.getData();
        List<ClinicaltrialsExcelModel> excelDataList = new ArrayList<>();
        int i = 0;
//        for (List<String> dataStrings : data) {
//            ClinicaltrialsExcelModel clinicaltrialsExcelModel = new ClinicaltrialsExcelModel();
//            Document rowDoc = Jsoup.parse(dataStrings.get(0));
//            clinicaltrialsExcelModel.setRowNum(rowDoc.select("body").text());
//
//            clinicaltrialsExcelModel.setIdentifier(dataStrings.get(1));
//
//            Document statusDoc = Jsoup.parse(dataStrings.get(2));
//            clinicaltrialsExcelModel.setStatus(statusDoc.select("body").text());
//
//            Document studyTitleDoc = Jsoup.parse(dataStrings.get(3));
//            clinicaltrialsExcelModel.setStudyTitle(studyTitleDoc.select("body").text());
//            String href = studyTitleDoc.select("a").attr("href");
//
//            Document conditionsDoc = Jsoup.parse(dataStrings.get(4));
//            Elements conditionsLi = conditionsDoc.select("li");
//            String condition = "";
//            for (int m = 1; m <= conditionsLi.size();m++) {
//                String text = conditionsLi.get(m - 1).text();
//                if (!StringUtils.isEmpty(text)){
//                    condition +=  m + "." + text + "\n";;
////                    condition += ";" + text;
//                }
//            }
//            clinicaltrialsExcelModel.setConditions(condition);
////            clinicaltrialsExcelModel.setConditions(condition.replaceFirst(";",""));
//
//            Document interventionsDoc = Jsoup.parse(dataStrings.get(5));
//            Elements interventionsLi = interventionsDoc.select("li");
//            String interventions = "";
//            for (int m = 1; m <= interventionsLi.size();m++) {
//                String text = interventionsLi.get(m - 1).text();
//                if (!StringUtils.isEmpty(text)){
////                    interventions += ";" + text;
//                    interventions += m + "." + text + "\n";
//                }
//            }
//            clinicaltrialsExcelModel.setInterventions(interventions);
////            clinicaltrialsExcelModel.setInterventions(interventions.replaceFirst(";",""));
//
//            Document locationsDoc = Jsoup.parse(dataStrings.get(dataStrings.size() - 2));
//            Elements locationsLi = locationsDoc.select("li");
//            String locations = "";
//            for (int m = 1; m <= locationsLi.size(); m++) {
//                String text = locationsLi.get(m - 1).text();
//                if (!StringUtils.isEmpty(text)){
////                    locations += ";" + text;
//                    locations += m + "." + text + "\n";
//                }
//            }
//            clinicaltrialsExcelModel.setLocations(locations);
////            clinicaltrialsExcelModel.setLocations(locations.replaceFirst(";",""));
//
//            clinicaltrialsExcelModel.setFirstPosted(dataStrings.get(dataStrings.size() - 5));
//            clinicaltrialsExcelModel.setLastUpdatePosted(dataStrings.get(dataStrings.size() - 4));
//
//            String detailUrl = prefix + href;
//            System.out.println("href-"+ i++ + ":" + detailUrl);
//
//            Map<String, String> map = new HashMap<>();
//            String substring = href.substring(href.indexOf("?") + 1).replace("+"," ");
//            for (String s1 : substring.split("&")) {
//                map.put(s1.split("=")[0], s1.split("=")[1]);
//            }
//
//            Connection connect = Jsoup.connect(detailUrl);
//            Connection.Response response = connect.data(map).method(Connection.Method.GET).ignoreContentType(true).execute();
//            Document document = response.parse();
//            Element studyDesignTable = document.select("table.ct-layout_table.tr-tableStyle.tr-studyInfo").first();
//            Elements studyDesignTableTr = studyDesignTable.select("tbody tr");
//            for (Element tr : studyDesignTableTr) {
//                String tdValue1 = tr.select("td").get(0).text().replace(" ","");
//                String tdValue2 = tr.select("td").get(1).text();
//                if (tdValue1.contains("StudyType")){
//                    clinicaltrialsExcelModel.setStudyType(tdValue2);
//                } else if (tdValue1.contains("Enrollment")){
//                    clinicaltrialsExcelModel.setActualEnrollment(tdValue2);
//                } else if (tdValue1.contains("ObservationalModel")){
//                    clinicaltrialsExcelModel.setObservationalModel(tdValue2);
//                } else if (tdValue1.contains("TimePerspective")){
//                    clinicaltrialsExcelModel.setTimePerspective(tdValue2);
//                } else if (tdValue1.contains("OfficialTitle")){
//                    clinicaltrialsExcelModel.setOfficialTitle(tdValue2);
//                }
//            }
//
//            // 元素下的子标签使用>表示
//            Elements outcomeMeasuresDivSons = document.select("div.tr-indent3 > div.ct-body3");
//            for (Element sonDiv : outcomeMeasuresDivSons) {
//                if (sonDiv.select("span").size() < 1){
//                    continue;
//                }
//                String spanValue = sonDiv.select("span").first().text();
//                if (spanValue.contains("Primary Outcome Measures")){
//                    String primaryOutcomeMeasures = "";
//                    Elements ol_li = sonDiv.select("ol li");
//                    int j = 0;
//                    for (Element element : ol_li) {
//                        j++;
//                        String li_div = element.select("li div").text();
//                        String replace = element.text().replace(li_div, "");
//                        primaryOutcomeMeasures += j + "." + replace + "\n";
//                    }
//                    clinicaltrialsExcelModel.setPrimaryOutcomeMeasures(primaryOutcomeMeasures);
//                } else if (spanValue.contains("Secondary Outcome Measures")){
//                    String secondaryOutcomeMeasures = "";
//                    Elements ol_li = sonDiv.select("ol li");
//                    int j = 0;
//                    for (Element element : ol_li) {
//                        j++;
//                        String li_div = element.select("li div").text();
//                        String replace = element.text().replace(li_div, "");
//                        secondaryOutcomeMeasures += j + "." + replace + "\n";
//                    }
//                    clinicaltrialsExcelModel.setSecondaryOutcomeMeasures(secondaryOutcomeMeasures);
//                }
//            }
//
//            Elements contactsAndLocationsDivs = document.select("div.ct-header3");
//            for (Element contactsAndLocationsDiv : contactsAndLocationsDivs) {
//                String divValue = contactsAndLocationsDiv.text();
//                if ("Sponsors and Collaborators".equals(divValue)){
//                    Element element = contactsAndLocationsDiv.nextElementSibling();
//                    clinicaltrialsExcelModel.setSponsorsAndCollaborators(element.text());
//                } else if (divValue.contains("Investigators")){
//                    Element element = contactsAndLocationsDiv.nextElementSibling();
//                    Element tableTr2 = element.select("table tr").get(1);
//                    clinicaltrialsExcelModel.setInvestigators(tableTr2.text());
//                }
//            }
//
//            excelDataList.add(clinicaltrialsExcelModel);
//        }

        String fileName = "C:\\Users\\hp\\Desktop\\钱多多\\qianduoduo.xlsx";
        write(fileName, ClinicaltrialsExcelModel.class).sheet().head(ClinicaltrialsExcelModel.class)
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy()).doWrite(excelDataList);
        System.out.println("生成excel成功");
        System.out.println(JSON.toJSONString(excelDataList));
    }
}
